package com.cyt.chattlibrary.message;

import android.net.Uri;
import android.os.Parcel;
import android.text.TextUtils;
import android.util.Log;

import com.cyt.chattlibrary.message.model.MessageContent;
import com.cyt.chattlibrary.message.model.UserInfo;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author QiQi
 * @ClassName: ImageMessage
 * @date 2018/10/22
 * @description
 */
public class ImageMessage extends MessageContent {

    public static final Creator<ImageMessage> CREATOR = new Creator<ImageMessage>() {
        @Override
        public ImageMessage createFromParcel(Parcel source) {
            return new ImageMessage(source);
        }

        @Override
        public ImageMessage[] newArray(int size) {
            return new ImageMessage[size];
        }
    };
    /**
     * 缩略图
     */
    private Uri mThumbUri;
    private Uri mLocalUri;
    private Uri mRemoteUri;
    private String mBase64;
    private boolean mIsFull;

    public ImageMessage() {
    }

    private ImageMessage(Uri thumbUri, Uri localUri) {
        this.mThumbUri = thumbUri;
        this.mLocalUri = localUri;
    }

    private ImageMessage(Uri thumbUri, Uri localUri, boolean original) {
        this.mThumbUri = thumbUri;
        this.mLocalUri = localUri;
        this.mIsFull = original;
    }

    protected ImageMessage(Parcel in) {
        this.mThumbUri = in.readParcelable(Uri.class.getClassLoader());
        this.mLocalUri = in.readParcelable(Uri.class.getClassLoader());
        this.mRemoteUri = in.readParcelable(Uri.class.getClassLoader());
        this.mBase64 = in.readString();
        this.mIsFull = in.readInt() == 1;
        this.setUserInfo(in.readParcelable(UserInfo.class.getClassLoader()));
    }


    public ImageMessage(byte[] data) {
        String jsonStr = new String(data);
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            if (jsonObject.has("imageUri")) {
                String uri = jsonObject.optString("imageUri");
                if (!TextUtils.isEmpty(uri)) {
                    this.setRemoteUri(Uri.parse(uri));
                }

                if (this.getRemoteUri() != null && this.getRemoteUri().getScheme() != null && this.getRemoteUri().equals("file")) {
                    this.setLocalUri(this.getRemoteUri());
                }

                if (jsonObject.has("content")) {
                    this.setBase64(jsonObject.getString("content"));
                }

                if (jsonObject.has("isFull")) {
                    this.setIsFull(jsonObject.optBoolean("isFull"));
                }

                if (jsonObject.has("user")) {
                    this.setUserInfo(this.parseJsonToUserInfo(jsonObject.getJSONObject("user")));
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public Uri getRemoteUri() {
        return mRemoteUri;
    }

    public void setRemoteUri(Uri remoteUri) {
        this.mRemoteUri = remoteUri;
    }

    public static ImageMessage obtain(Uri thumbUri, Uri localUri) {
        return new ImageMessage(thumbUri, localUri);
    }

    public static ImageMessage obtain(Uri thumbUri, Uri localUri, boolean isFull) {
        return new ImageMessage(thumbUri, localUri, isFull);
    }

    public static ImageMessage obtain() {
        return new ImageMessage();
    }

    public Uri getThumUri() {
        return mThumbUri;
    }

    public void setThumUri(Uri thumUri) {
        this.mThumbUri = thumUri;
    }

    public String getBase64() {
        return mBase64 == null ? "" : mBase64;
    }

    public void setBase64(String base64) {
        this.mBase64 = base64;
    }

    public boolean isIsFull() {
        return mIsFull;
    }

    public void setIsFull(boolean isFull) {
        this.mIsFull = isFull;
    }

    @Override
    public byte[] encode() {
        JSONObject jsonObject = new JSONObject();
        try {
            if (!TextUtils.isEmpty(this.mBase64)) {
                jsonObject.put("content", this.mBase64);
            } else {
                Log.d("ImageMessage", "base64 is null");
            }

            if (this.mRemoteUri != null) {
                jsonObject.put("imageUri", this.mRemoteUri.toString());
            } else if (this.getLocalUri() != null) {
                jsonObject.put("imageUri", this.mLocalUri.toString());
            }

            jsonObject.put("isFull", this.mIsFull);

            if (this.getJSONUserInfo() != null) {
                jsonObject.putOpt("user", this.getJSONUserInfo());
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        this.mBase64 = null;
        return jsonObject.toString().getBytes();
    }

    public Uri getLocalUri() {
        return mLocalUri;
    }

    public void setLocalUri(Uri localUri) {
        this.mLocalUri = localUri;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(this.mThumbUri, flags);
        dest.writeParcelable(this.mLocalUri, flags);
        dest.writeParcelable(this.mRemoteUri, flags);
        dest.writeString(this.mBase64);
        dest.writeInt(this.mIsFull ? 1 : 0);
        dest.writeParcelable(this.getUserInfo(), flags);
    }
}
